<?php
/**
 * App routes
 * https://github.com/fdisotto/SlimBlog
 */

// Home / base
$app->get('/', function() use ($app){
    $app->redirect($app->urlFor('login'));
})->name('home');

$app->group('/dashboard', function () use ($app, $authenticate){
    
    $app->get('/', $authenticate($app), function() use ($app) {
        $app->render('dashboard.html.twig', array(
            'pagename' => 'Dashboard',
            'email' => $_SESSION['email']
        ));
    })->name('dashboard');
    
    $app->group('/program', function () use ($app, $authenticate){
        
        $app->get('/', $authenticate($app), function() use ($app){
            $flash = $app->view()->get('flash');
            $success = isset($flash['success']) ? $flash['success'] : '';
            $events = Events::get_all();
            $venues = Venues::get_all();
            $app->render('program.html.twig', array(
                'email' => $_SESSION['email'],
                'pagename' => 'Program',
                'events' => $events,
                'venues' => $venues,
                'success' => $success
            ));
        })->name('program');
        
        $app->get('/edit/event/:id', $authenticate($app), function($id) use ($app){
            $event = Events::find($id);
            $venues = Venues::get_all();
            $days = Events::get_days();
            $categories = Events::get_categories();
            $flash = $app->view()->get('flash');
            $error = isset($flash['error']) ? $flash['error'] : '';
            $app->render('event-edit.html.twig', array(
                'pagename' => 'Program',
                'event' => $event,
                'venues' => $venues,
                'days' => $days,
                'pageheader' => 'Event editor',
                'error' => $error,
                'categories' => $categories
            ));
        })->name('edit-event');
        
        $app->post('/edit/event/:id', $authenticate($app), function($id) use ($app){
            $event = Events::find($id);
            if($event){
                $event->event_day = $app->request->post('event-day');
                $event->event_start_time = $app->request->post('event-start-time');
                $event->event_end_time = $app->request->post('event-end-time');
                $event->event_description = $app->request->post('event-description');
                $event->event_details = $app->request->post('event-details');
                $event->venue_id = $app->request->post('event-venue');
                $event->event_category = $app->request->post('event-category');
                $event->save();
            }    
            else {
                $app->flash('error', 'Update failed.');
                $app->redirect($app->urlFor('edit-event', array('id' => $id)));
            }
            $app->flash('success', "Event with id {$id} was updated.");
            $app->redirect($app->urlFor('program'));
        })->name('update-event');
        
        $app->post('/remove/event/:id', $authenticate($app), function($id) use ($app){
            $event = Events::find($id);
            if($event){
                $event->delete();
                $app->flash('success', "Event with id {$id} was removed.");
                $app->redirect($app->urlFor('program'));
            }
            else {
                $app->flash('error', 'Remove failed.');
                $app->redirect($app->urlFor('edit-event', array('id' => $id)));
            }   
        })->name('remove-event');
        
        $app->get('/new/event', $authenticate($app), function() use ($app){
            $venues = Venues::get_all();
            $days = Events::get_days();
            $categories = Events::get_categories();
            $flash = $app->view()->get('flash');
            $error = isset($flash['error']) ? $flash['error'] : '';
            $app->render('event-new.html.twig', array(
                'pagename' => 'Program',
                'pageheader' => 'New event',
                'venues' => $venues,
                'days' => $days,
                'error' => $error,
                'categories' => $categories
            ));
        })->name('new-event');
        
        $app->post('/new/event', $authenticate($app), function() use ($app){
            $event = new Events;
            if($event){
                $event->event_day = $app->request->post('event-day');
                $event->event_start_time = $app->request->post('event-start-time');
                $event->event_end_time = $app->request->post('event-end-time');
                $event->event_description = $app->request->post('event-description');
                $event->event_details = $app->request->post('event-details');
                $event->venue_id = $app->request->post('event-venue');
                $event->event_category = $app->request->post('event-category');
                $event->save();
                $app->flash('success', "Event successfully created.");
                $app->redirect($app->urlFor('program'));
            }
            else {
                $app->flash('error', 'Failed to create a new event.');
                $app->redirect($app->urlFor('new-event'));
            }
        })->name('create-event');
        
        $app->get('/edit/venue/:id', $authenticate($app), function($id) use ($app){
            $venue = Venues::find($id);
            $flash = $app->view()->get('flash');
            $error = isset($flash['error']) ? $flash['error'] : '';
            $app->render('venue-edit.html.twig', array(
                'pagename' => 'Program',
                'venue' => $venue,
                'pageheader' => 'Venue editor',
                'error' => $error
            ));
        });
        
        $app->post('/edit/venue/:id', $authenticate($app), function($id) use ($app){
            $venue = Venues::find($id);
            if($venue){
                $venue->venue_name = $app->request->post('venue-name');
                $event->venue_order_number = $app->request->post('venue-order-number');
                $venue->save();
            }    
            else {
                $app->flash('error', 'Update failed.');
                $app->redirect($app->urlFor('edit-venue', array('id' => $id)));
            }
            $app->flash('success', "Venue with id {$id} was updated.");
            $app->redirect($app->urlFor('program'));
        })->name('update-venue');
        
        $app->post('/remove/venue/:id', $authenticate($app), function($id) use ($app){
            $venue = Venues::find($id);
            if($venue){
                $venue->delete();
                $app->flash('success', "Venue with id {$id} was removed.");
                $app->redirect($app->urlFor('program'));
            }
            else {
                $app->flash('error', 'Remove failed.');
                $app->redirect($app->urlFor('edit-venue', array('id' => $id)));
            }   
        })->name('remove-venue');
        
        $app->get('/new/venue', $authenticate($app), function() use ($app){
            $flash = $app->view()->get('flash');
            $error = isset($flash['error']) ? $flash['error'] : '';
            $app->render('venue-new.html.twig', array(
                'pagename' => 'Program',
                'pageheader' => 'New venue',
                'error' => $error
            ));
        })->name('new-venue');
        
        $app->post('/new/venue', $authenticate($app), function() use ($app){
            $venue = new Venues;
            if($venue){
                $venue->venue_name = $app->request->post('venue-name');
                $venue->venue_order_number = $app->request->post('venue-order-number');
                $venue->save();
                $app->flash('success', "Venue successfully created.");
                $app->redirect($app->urlFor('program'));
            }
            else {
                $app->flash('error', 'Failed to create a new venue.');
                $app->redirect($app->urlFor('new-venue'));
            }
        })->name('create-venue');
    });
    
    

    $app->group('/eatdrink', function () use ($app, $authenticate){
        
        $app->get('/', $authenticate($app), function() use ($app){
            $flash = $app->view()->get('flash');
            $success = isset($flash['success']) ? $flash['success'] : '';
            $foods = FoodPlaces::get_all();
            $app->render('eatdrink.html.twig', array(
                'pagename' => 'Eatdrink',
                'success' => $success,
                'foods' => $foods
            ));
        })->name('eatdrink');
       
        $app->get('/edit/food/:id', $authenticate($app), function($id) use ($app){
            $flash = $app->view()->get('flash');
            $error = isset($flash['error']) ? $flash['error'] : '';
            $food = FoodPlaces::find($id);
            $app->render('food-edit.html.twig', array(
                'pagename' => 'Eatdrink',
                'pageheader' => 'Food place editor',
                'error' => $error,
                'food' => $food
            ));
        })->name('edit-food');
        
        $app->post('/edit/food/:id', $authenticate($app), function($id) use ($app){
            $food = FoodPlaces::find($id);
            if($food){
                $food->food_place_name = $app->request->post('food-name');
                $food->food_place_description = $app->request->post('food-description');
                $food->save();
            }    
            else {
                $app->flash('error', 'Update failed.');
                $app->redirect($app->urlFor('edit-food', array('id' => $id)));
            }
            $app->flash('success', "Place with id {$id} was updated.");
            $app->redirect($app->urlFor('eatdrink'));
        })->name('update-food');
        
        $app->get('/new/food', $authenticate($app), function() use ($app){
            $flash = $app->view()->get('flash');
            $error = isset($flash['error']) ? $flash['error'] : '';
            $app->render('food-new.html.twig', array(
                'pagename' => 'Program',
                'pageheader' => 'New food place',
                'error' => $error
            ));
        })->name('new-food');
        
        $app->post('/new/food', $authenticate($app), function() use ($app){
            $food = new FoodPlaces;
            if($food){
                $food->food_place_name = $app->request->post('food-name');
                $food->food_place_description = $app->request->post('food-description');
                $food->save();
            }    
            else {
                $app->flash('error', 'Update failed.');
                $app->redirect($app->urlFor('edit-food', array('id' => $id)));
            }
            $app->flash('success', "Food place successfully created.");
            $app->redirect($app->urlFor('eatdrink'));
        })->name('create-food');
        
        $app->post('/remove/food/:id', $authenticate($app), function($id) use ($app){
            $food = FoodPlaces::find($id);
            if($food){
                $food->delete();
                $app->flash('success', "Place with id {$id} was removed.");
                $app->redirect($app->urlFor('eatdrink'));
            }
            else {
                $app->flash('error', 'Remove failed.');
                $app->redirect($app->urlFor('edit-food', array('id' => $id)));
            }
        })->name('remove-food');
    });

    $app->group('/info', function () use ($app){
    
        $app->get('/', function () use ($app){
            $app->render('info.html.twig', array(
               'pagename' => 'Info'
            ));
        })->name('info');
        
    });
    
});
